package nl.windgazer.arachne.services.messages;

import java.util.Locale;

import org.apache.log4j.Logger;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.model.ComponentModel;
import org.apache.tapestry5.services.Environment;
import org.apache.tapestry5.services.InvalidationEventHub;
import org.apache.tapestry5.services.messages.ComponentMessagesSource;
import org.apache.tapestry5.services.pageload.ComponentResourceSelector;

public class ComponentMessagesSourceInterceptor implements
		ComponentMessagesSource {
	
	private ComponentMessagesSource cms;

	private Logger logger = Logger.getLogger(ComponentMessagesSourceInterceptor.class);

	private Environment environment; 

	public ComponentMessagesSourceInterceptor(ComponentMessagesSource cms, Environment environment) {
		this.cms = cms;
		this.environment = environment;
	}

	public InvalidationEventHub getInvalidationEventHub() {
		return cms.getInvalidationEventHub();
	}

	public Messages getMessages(ComponentModel componentModel, Locale locale) {
		if (logger.isDebugEnabled())
			logger.debug("Intercepting messages for " + componentModel.getComponentClassName());
		return new MessagesInterceptor( cms.getMessages(componentModel, locale), environment, locale );
	}

	public Messages getApplicationCatalog(Locale arg0) {
		return cms.getApplicationCatalog(arg0);
	}

	public Messages getMessages(ComponentModel arg0,
			ComponentResourceSelector arg1) {
		return new MessagesInterceptor( cms.getMessages(arg0, arg1), environment, arg1.locale );
	}

}
